package ctm.javacode;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/certificate.do")

public class certificate extends HttpServlet {
	PrintWriter out;
	
	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		out = response.getWriter();
		
		if (request.getParameter("type").equals("add"))
			PrintSymptom(request.getParameter("inputs"), request.getParameter("symptom"));
		else if (request.getParameter("type").equals("print"))
		{
			Print1(request.getParameter("inputs"));
			Print2(request.getParameter("inputs"));
			Print3(request.getParameter("inputs"));
		}
			
		
		out.flush();  
		out.close();
	}

	private void Print3(String str) {
		int[] temp = new int[3];
		String[] tempString = new String[3];
		for ( int i = 0; i < 3; ++i )
		{
			temp[i] = 0;
			tempString[i] = "";
		}
		
		if ( str.contains("面色白") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "面色白 ";
			temp[2]++;
			tempString[2] = tempString[2] + "面色白 ";
		}
		
		if ( str.contains("神疲") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "神疲 ";
		}
		
		if ( str.contains("自汗") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "自汗 ";
		}
		
		if ( str.contains("寡言") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "寡言 ";
		}
		
		if ( str.contains("聲弱") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "聲弱 ";
		}
		
		if ( str.contains("食慾不振") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "食慾不振 ";
		}
		
		if ( str.contains("大便泄瀉") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "大便泄瀉 ";
		}
		
		if ( str.contains("呼吸微弱") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "呼吸微弱 ";
		}
		
		if ( str.contains("四肢厥冷") )
		{
			temp[1]++;
			tempString[1] = tempString[1] + "四肢厥冷 ";
		}
		
		if ( str.contains("畏寒") )
		{
			temp[1]++;
			tempString[1] = tempString[1] + "畏寒 ";
		}
		
		if ( str.contains("痰質濃") )
		{
			temp[1]++;
			tempString[1] = tempString[1] + "痰質濃 ";
		}
		
		String outputStr = new String("證類(症狀比率)：症狀\n");
		for ( int i = 0; i < 3; ++i)
		{
			if (temp[i] != 0)
				outputStr = outputStr + Output3(i, temp[i], tempString[i]);
		}
		
		System.out.println(outputStr);
		out.print("#output3:" + outputStr + "|");
	}

	private String Output3(int i, int num, String string) {
		String str = new String("");
		if ( i == 0 )
			str = "氣虛(" + num + "/11)：" + string + "\n";
		else if ( i == 1 )
			str = "陽虛(" + num + "/5)：" + string + "\n";
		else if ( i == 2 )
			str = "血虛(" + num + "/9)：" + string + "\n";
		
		return str;
	}

	private void Print2(String str) {
		int[] num1 = new int[3];
		int[] num2 = new int[3];
		String[][] temp = new String[3][3];
		String[] temp1 = new String[3];
		
		for ( int i = 0; i < 3; ++i )
		{
			num1[i] = 0;
			num2[i] = 0;
			for ( int j = 0; j < 3; ++j )
				temp[i][j] = "";
		}
		
		if ( str.contains("神疲") )
		{
			num1[0]++;
			num2[0]++;
			temp[0][0] = temp[0][0] + "神疲 ";
		}
		
		if ( str.contains("寡言") || str.contains("聲弱") || str.contains("呼吸微弱") )
		{
			num1[0]++;
			if ( str.contains("寡言") )
			{
				num2[0]++;
				temp[0][1] = temp[0][1] + "寡言 ";
			}
			
			if ( str.contains("聲弱") )
			{
				num2[0]++;
				temp[0][1] = temp[0][1] + "聲弱 ";
			}
			
			if ( str.contains("呼吸微弱") )
			{
				num2[0]++;
				temp[0][1] = temp[0][1] + "呼吸微弱 ";
			}
		}

		if ( str.contains("自汗") )
		{
			num1[0]++;
			num2[0]++;
			temp[0][2] = temp[0][2] + "自汗 ";
		}
		
		if ( str.contains("食慾不振") )
		{
			num1[1]++;
			num2[1]++;
			temp[1][0] = temp[1][0] + "食慾不振 ";
		}
		
		if ( str.contains("大便泄瀉") )
		{
			num1[1] = num1[1] + 2;
			num2[1]++;
			temp[1][1] = temp[1][1] + "大便泄瀉 ";
			temp[1][2] = temp[1][2] + "大便泄瀉 ";
		}
		
		if ( str.contains("痰質濃") )
		{
			num1[2]++;
			num2[1]++;
			temp[2][0] = temp[2][0] + "痰質濃 ";
		}
		
		String output = new String("證門(特徵比率)(症狀比率)\n\t特徵：症狀\n");
		for ( int i = 0; i < 3; ++i)
		{
			if (num1[i] != 0)
				output = output + Output2(i, num1[i], num2[i], temp);
		}
		
		out.print("#output2:" + output+ "|");
	}


	private String Output2(int i, int num1, int num2, String[][] temp) {
		String str = new String("");
		if ( i == 0 )
		{
			str = "虛(" + num1 + "/4)(" + num2 + "/15)\n";
			for ( int j = 0; j < 3; ++j )
			{
				if ( !temp[i][j].equals("") )
					if ( j == 0 )
						str = str + "\t少神：" + temp[i][j] + "\n";
					else if ( j == 1 )
						str = str + "\t形氣衰減：" + temp[i][j] + "\n";
					else if ( j == 2 )
						str = str + "\t多汗：" + temp[i][j] + "\n";
			}
		}
		else if ( i == 1 )
		{
			str = "食(" + num1 + "/6)(" + num2 + "/13)\n";
			for ( int j = 0; j < 3; ++j )
			{
				if ( !temp[i][j].equals("") )
					if ( j == 0 )
						str = str + "\t惡食：" + temp[i][j] + "\n";
					else if ( j == 1 )
						str = str + "\t泄利酸臭：" + temp[i][j] + "\n";
					else if ( j == 2 )
						str = str + "\t吐利兼作：" + temp[i][j] + "\n";
			}
		}
		else if ( i == 2 )
			str = "燥(1/2)(1/15)\n\t乾澀：痰質濃\n";

		return str;
	}

	private void Print1(String str) {
		int[] temp = new int[6];
		String[] tempString = new String[6];
		
		for ( int i = 0; i < 6; ++i )
		{
			temp[i] = 0;
			tempString[i] = "";
		}
		
		if ( str.contains("咳嗽") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "咳嗽 ";
		}
		
		if ( str.contains("痰質濃") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "痰質濃 ";
		}
		if ( str.contains("咳喘") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "咳喘 ";
		}
		
		if ( str.contains("呼吸微弱") )
		{
			temp[0]++;
			tempString[0] = tempString[0] + "呼吸微弱 ";
		}
		
		if ( str.contains("面色萎黃") )
		{
			temp[1]++;
			tempString[1] = tempString[1] + "面色萎黃 ";
		}
		
		if ( str.contains("大便泄瀉") )
		{
			temp[1]++;
			tempString[1] = tempString[1] + "大便泄瀉 ";
			temp[2]++;
			tempString[2] = tempString[2] + "大便泄瀉 ";
		}
		
		if ( str.contains("四肢厥冷") )
		{
			temp[3]++;
			tempString[3] = tempString[3] + "四肢厥冷 ";
			temp[5]++;
			tempString[5] = tempString[5] + "四肢厥冷 ";
		}
		
		if ( str.contains("食慾不振") )
		{
			temp[4]++;
			tempString[4] = tempString[4] + "食慾不振 ";
		}
		
		String outputStr = new String("臟腑(症狀比率)：症狀\n");
		for ( int i = 0; i < 6; ++i)
		{
			if (temp[i] != 0)
				outputStr = outputStr + Output1(i, temp[i], tempString[i]);
		}
		
//		System.out.println(outputStr);
		out.print("#output1:" + outputStr + "|");
		//out.print(outputStr);
	}

	private String Output1(int i, int num, String string) {
		String str = new String("");
		if ( i == 0 )
			str = "肺(" + num + "/18)：" + string + "\n";
		else if ( i == 1 )
			str = "脾(" + num + "/13)：" + string + "\n";
		else if ( i == 2 )
			str = "腸(" + num + "/9)：" + string + "\n";
		else if ( i == 3 )
			str = "腎(" + num + "/11)：" + string + "\n";
		else if ( i == 4 )
			str = "胃(" + num + "/16)：" + string + "\n";
		else if ( i == 5 )
			str = "肝(" + num + "/22)：" + string + "\n";
		
		return str;
	}

	private void PrintSymptom(String str1, String str2) {
		if (str1 == null)
			out.print(str2);
		else if ( str1.contains(str2) ) 
			out.print("重複");
		else
			out.print(str2);
	}
}
